<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <router-link to="/conf/system">
          <el-button type="primary" size="mini" @click="handleConf"
            >系统配置</el-button
          >
        </router-link>
      </el-col>
      <el-col :span="1.5">
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="getHostList"
        ></el-button>
      </el-col>
    </el-row>
    <el-table
      v-loading="loading"
      :data="hostList"
      :row-class-name="rowClassName"
    >
      <el-table-column type="index" label="序号"> </el-table-column>
      <el-table-column
        label="主机"
        prop="host"
        align="center"
        min-width="120"
      />
      <el-table-column label="操作系统" prop="type" align="center" />
      <el-table-column label="SSH是否启用" align="center">
        <template slot-scope="scope">
          {{ scope.row.ssh ? "启用" : "未启用" }}
        </template>
      </el-table-column>
      <el-table-column label="是否存在代理" align="center">
        <template slot-scope="scope">
          {{ scope.row.existProxy ? "存在" : "不存在" }}
        </template>
      </el-table-column>
      <el-table-column label="代理是否在线" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-check"
            v-show="scope.row.proxyOnline"
            style="color: limegreen"
            >在线</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-close"
            v-show="!scope.row.proxyOnline"
            disabled
            >不在线</el-button
          >
        </template>
      </el-table-column>
      <el-table-column
        label="上线时间"
        prop="uptime"
        align="center"
        min-width="150"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        min-width="150"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="startProxyHandler(scope.row)"
            >启动代理</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="stopProxyHandler(scope.row)"
            >停止代理</el-button
          >
          <el-button size="mini" type="text" @click="logHandler(scope.row)"
            >日志</el-button
          >
        </template>
      </el-table-column>
      <el-table-column
        label="远程"
        align="center"
        class-name="small-padding fixed-width"
        min-width="100"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="termHandler(scope.row)"
            v-if="scope.row.type == 'linux'"
            >终端</el-button
          >
          <el-button size="mini" type="text" @click="fileHandler(scope.row)"
            >文件浏览</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import { getHostList, startProxy, stopProxy } from "@/api/console";
export default {
  name: "host",
  data() {
    return {
      loading: false,
      hostList: [],
    };
  },
  created() {
    this.getHostList();
  },
  methods: {
    getHostList() {
      this.loading = true;
      getHostList()
        .then((res) => {
          this.hostList = res.data;
          this.loading = false;
        })
        .catch((msg) => {
          this.loading = false;
        });
    },
    rowClassName({ row, rowIndex }) {
      row.index = rowIndex;
    },
    startProxyHandler(row) {
      this.$confirm("是否确认在主机" + row.host + "启动代理?", "确定", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return startProxy(row.host);
        })
        .then((res) => {
          this.msgSuccess("操作成功,请刷新");
        })
        .catch((msg) => {
          console.log(msg);
        });
    },
    stopProxyHandler(row) {
      this.$confirm("是否确认在主机" + row.host + "停止代理?", "确定", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return stopProxy(row.host);
        })
        .then((res) => {
          this.msgSuccess("操作成功,请刷新");
        })
        .catch((msg) => {
          console.log(msg);
        });
    },
    logHandler(row) {
      const dir = "/proxy";
      const path = row.homePath + "/logs/proxy/proxy.log";
      this.$store.commit("log/SET_HOST", row.host);
      this.$store.commit("log/SET_PATH", path);
      this.$store.commit("log/SET_DIR", dir);
      this.$router.push("/service/log");
    },
    handleConf() {},
    termHandler(row) {
      const host = row.host;
      let endpoint = process.env.VUE_APP_BASE_API + "/webssh";
      if (process.env.VUE_APP_BASE_API.indexOf("http://") == -1) {
        endpoint = "ws://" + window.location.host + "/jwater/webssh";
      } else {
        endpoint = endpoint
          .replace("http://", "ws://")
          .replace("https://", "wss://");
      }

      window.open(
        "/jwater/webssh/page/webssh.html?host=" +
          encodeURIComponent(host) +
          "&endpoint=" +
          encodeURIComponent(endpoint)
      );
    },
    fileHandler(row) {
      this.$router.push("/system/browse?host=" + row.host);
    },
  },
};
</script>

